﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Configuration;
using System.Data;

namespace Valgmaskine_Website.login
{
    public partial class _default : System.Web.UI.Page
    {
        string conn = ConfigurationManager.ConnectionStrings["ValgmaskineDB"].ConnectionString;



        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["loggedPå"] != null)
            {
                Response.Redirect("/");
            }

        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string u = txtUser.Text;
            string p = txtPass.Text;
            
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM LoginUser WHERE username=@username AND pass=@pass"))
            {
                SqlConnection dataConn = new SqlConnection(conn);
                cmd.Connection = dataConn;
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("username", u);
                cmd.Parameters.AddWithValue("pass", p);
                dataConn.Open();
                SqlDataReader rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {
                    string brugerNavn = rdr["username"].ToString();
                    if (brugerNavn.Length > 0)
                    {
                        Session["loggedPå"] = "1";
                        dataConn.Close();
                        Response.Redirect("/");
                    }
                    else
                    {
                        dataConn.Close();
                        Response.Redirect("/login/");
                    }
                }

                
               
            }
        }
    }
}